
use "created_dta\whole_samp_w_outcomes", clear
keep if year_arrival >= 2001 | (_merge_analy ==3 & year_arrival_as >= 2001)
g an_samp_dum = _merge_analy == 2 | _merge_analy ==3
egen mos_post_yr_terms = group(init_pmos post_orgraa init_terms year_arrival)
egen mos_tc_post_yr_terms = group(init_pmos_cons post_orgraa init_terms year_arrival)
egen mos_tc_post_yr_terms_as = group(init_pmos_cons post_orgraa init_terms year_arrival an_samp_dum)
egen mos_tc_post_yr_terms_an_samp = group(init_pmos_cons_as post_orgraa_as init_terms_as year_arrival_as)
egen mos_tc_post_qtr_terms_as = group(init_pmos_cons post_orgraa init_terms qtr  an_samp_dum)
g all = 1 
g terms3 = init_terms == "3"
g anydepc = init_nrdepc > 0 & init_nrdepc < . 

forvalues y = 1(1)36 {
 g noncom_death_by_`y' = ndi_death_by_`y' - combat_death_by_`y'
 replace noncom_death_by_`y' = 0 if noncom_death_by_`y' == -1
 g ndi_nondespnoncom_by_`y' = ndi_death_by_`y' - combat_death_by_`y' - ndi_despair2_by_`y'
g despairv3_by_`y' = (ndi_drug2_by_`y' == 1 | ndi_suicide_plus2_by_`y' ==1  )
 g intentdrug_by_`y' = (ndi_drug_self2_by_`y' == 1 | ndi_drug_und2_by_`y' == 1) 
  g ndi_suic_by_`y' = ndi_suicide_plus2_by_`y'
 }
 

g mths_dep_win_3yr = deployed_0 + deployed_1 + deployed_2 + deployed_3 ///
+ deployed_4 + deployed_5 + deployed_6 + deployed_7 ///
+ deployed_8 + deployed_9 + deployed_10 + deployed_11 ///
+ deployed_12 + deployed_13 + deployed_14 + deployed_15 ///
+ deployed_16 + deployed_17 + deployed_18 + deployed_19 ///
+ deployed_20 + deployed_21 + deployed_22 + deployed_23 ///
+ deployed_24 + deployed_25 + deployed_26 + deployed_27 ///
+ deployed_28 + deployed_29 + deployed_30 + deployed_31 ///
+ deployed_32 + deployed_33 + deployed_34 + deployed_35

summ mths_dep_win_3yr if dep_win_3yr, det

bys dmsl_tc_first qtr : egen meandep = mean(mths_dep_win_3yr)  if an_samp_dum
bys dmsl_tc_first qtr: egen ct = total(1) if an_samp_dum
g instrument_mths_dep_qtr = ((meandep*ct) - mths_dep_win_3yr)/(ct-1) if an_samp_dum
drop meandep ct 

bys dmsl_tc_first_as qtr_as : egen meandep = mean(mths_dep_win_3yr_as)  if an_samp_dum
bys dmsl_tc_first_as qtr_as: egen ct = total(1) if an_samp_dum
g instrument_mths_dep_qtr_as = ((meandep*ct) - mths_dep_win_3yr_as)/(ct-1) if an_samp_dum
drop meandep ct 

bys dmsl_tc_first month_arrival : egen meandep = mean(mths_dep_win_3yr) if an_samp_dum
bys dmsl_tc_first month_arrival: egen ct = total(1) if an_samp_dum 
g instrument_mths_dep_mth = ((meandep*ct) - mths_dep_win_3yr)/(ct-1) if an_samp_dum
drop meandep ct 

bys dmsl_tc_first year_arrival : egen meandep = mean(mths_dep_win_3yr) if an_samp_dum
bys dmsl_tc_first year_arrival: egen ct = total(1) if an_samp_dum
g instrument_mths_dep_yr = ((meandep*ct) - mths_dep_win_3yr)/( ct-1) if an_samp_dum
drop meandep ct 

bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr) if an_samp_dum
bys dmsl_tc_first qtr: egen ct = total(1) if an_samp_dum
g instrument_any_dep_qtr = ((meandep*ct) - dep_win_3yr)/(ct-1) if an_samp_dum
drop meandep ct 

g init_afqsc_2 = init_afqsc^2
g init_afqsc_3 = init_afqsc^3

foreach ls in cl co el fa gm gt mm of sc { 
	g `ls'_mi = ace_first_asvab`ls' == .
	replace ace_first_asvab`ls' = 0 if ace_first_asvab`ls' == .
}

bys dmsl_tc_first qtr: egen ct = total(1) 

gen kia_by_12_adj = kia_by_12 
replace kia_by_12_adj = 0 if dep_win_3yr == 0  
bys dmsl_tc_first qtr: egen meankia = mean(kia_by_12_adj) 
g kiarate = ((meankia*ct) - kia_by_12_adj)/(ct - 1)
		assert kiarate>-.000001
		replace kiarate = 0 if kiarate<0 & kiarate>-.000001
gen wia_by_12_adj = wia_by_12 
replace wia_by_12_adj = 0 if dep_win_3yr == 0 
bys dmsl_tc_first qtr: egen meanwia = mean(wia_by_12_adj) 
gen wiarate = ((meanwia*ct) - wia_by_12_adj)/(ct-1)
		assert wiarate>-.000001 
		replace wiarate = 0 if wiarate<0 & wiarate>-.000001

	gen combat_death_by_12_adj = combat_death_by_12 
replace combat_death_by_12_adj = 0 if dep_win_3yr == 0  
bys dmsl_tc_first qtr: egen meancombat = mean(combat_death_by_12_adj) 
g comrate = ((meancombat*ct) - combat_death_by_12_adj)/(ct - 1)
		assert comrate>-.000001
		replace comrate = 0 if comrate<0 & comrate>-.000001
	
	gen cas_by_12_adj = (combat_death_by_12_adj==1|wia_by_12_adj==1)
	bys dmsl_tc_first qtr: egen meancas = mean(cas_by_12_adj) 
	gen casrate = ((meancas*ct) - cas_by_12_adj)/(ct-1)

foreach n in cas kia wia com {
	gen `n'rate_notsd=`n'rate
	sum `n'rate
	replace `n'rate = (`n'rate - r(mean))/r(sd)
}

drop ct 

replace ${NEWINST} = 0 if an_samp_dum == 0

foreach n in kia wia cas com {
	gen ${NEWINST}_`n'rate = ${NEWINST} * `n'rate
	gen ${NEWENDOG}_`n'rate = ${NEWENDOG} * `n'rate

}

	gen ${NEWINST}_casnotsd = ${NEWINST} * `n'casrate_notsd *100
	gen ${NEWENDOG}_`n'casnotsd = ${NEWENDOG} * `n'casrate_notsd * 100
	gen casnotsd = casrate_notsd * 100


foreach var of varlist combat_death_by_* noncom_death_by_* ndi_war_by_* ndi_despair2_by_* ndi_suicide_plus2_by_* ndi_drug2_by_* ndi_mv_acc2_by_* ndi_other2_by_* ndi_firearm2_by_* ndi_assault2_by_*  anydisability_in_* anyvadc_in_*  anyptsd_in_* anytbi_in_* ndi_death_by_* kia_by_* wia_by_* { 
	count if `var' != 0 & `var' != 1 & kia_by_2!=. 
	if r(N) == 0 replace `var' = `var' * 100
}

compress
save "created_dta\whole_samp_w_outcomes_final", replace
